package labb3.FileHandling;

import javax.swing.filechooser.FileFilter;
import java.io.File;

/**
 * MediaFileFilter is an abstract class used by JFileChooser to filter the set
 * of files shown to the user.
 *
 * @author Mikko Puustinen
 * @version 0.5, 2007-okt-17
 * @see javax.swing.filechooser.FileFilter
 * @see javax.swing.JFileChooser
 */
public abstract class MediaFileFilter extends FileFilter {

    /**
     * Extending classes can specify which file extension that should be
     * returned.
     *
     * @return file extension as a String.
     */
    protected abstract String getFilterExtension();

    /**
     * Returns the extension of the File.
     *
     * @param f the File to get the extension from.
     *
     * @return the extension (without a dot). If no extension is found this will
     *         return null.
     */
    String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

        if (i > 0 && i < s.length() - 1) {
            ext = s.substring(i + 1).toLowerCase();
        }
        return ext;
    }
}
